/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.siga.modelo.DAO;

import com.siga.modelo.entidad.Carrera;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.Query;

/**
 *
 * @author Ricardo
 */
public class CarreraDAO {
    private EntityManagerFactory emf;
    EntityManager em;

    public CarreraDAO() {
        emf=Persistence.createEntityManagerFactory("AppSigaPPU");
        em=emf.createEntityManager();
    }
    public void closeEntityManager(){
        em.close();
    }
    public boolean insertarCarrera(Carrera c){
        boolean b=false;
        try{
            em.getTransaction().begin();
            em.persist(c);
            em.getTransaction().commit();
            b=true;
        }catch(Exception e){
            em.getTransaction().rollback();
        }
        return b;
    }
    
    public Carrera buscarCarrera(String codigo){
        Carrera c= em.find(Carrera.class, codigo);
        return c;
    }
    
    public List<Carrera> getListaCarreras(){
        Query q=em.createQuery("Select c from Carrera c");//em.createNativeQuery(....);
        return q.getResultList();//JPQL
    }
    
    public boolean eliminarCarrera(String codigo){
        boolean b=false;
        try{
            em.getTransaction().begin();
            Carrera c= em.find(Carrera.class, codigo);
            em.remove(c);
            em.getTransaction().commit();
            b=true;
        }catch(Exception e){
            em.getTransaction().rollback();
        }
        return b;
    }
}